#include "Timer.h"


Timer::Timer(void)
	:secondsPerCount(0.0), deltaTime(-1.0), baseTime(0), pausedTime(0), prevTime(0), currTime(0), stopped(false)
{
	__int64 countsPerSec;
	QueryPerformanceFrequency((LARGE_INTEGER*)&countsPerSec);
	secondsPerCount = 1.0 / (double)countsPerSec;
}


Timer::~Timer(void)
{
}

float Timer::GetGameTime()
{
	if(stopped)
	{
		return (float)((stopTime - baseTime) * secondsPerCount);
	}
	else
	{
		return (float)(((currTime - pausedTime) - baseTime) * secondsPerCount);
	}
}

float Timer::GetDeltaTime()
{
	return float(deltaTime);
}

void Timer::Reset()
{
	__int64 currTimeTmp;
	QueryPerformanceCounter((LARGE_INTEGER*)&currTimeTmp);

	baseTime = currTimeTmp;
	prevTime = currTimeTmp;
	stopTime = 0;
	stopped = false;
}

void Timer::Start()
{
	__int64 startTime;
	QueryPerformanceCounter((LARGE_INTEGER*)&startTime);

	if(stopped)
	{
		pausedTime += (startTime - stopTime);
		stopTime = 0;
		stopped = false;
	}
}

void Timer::Stop()
{
	if(!stopped)
	{
		__int64 currTimeTmp;
		QueryPerformanceCounter((LARGE_INTEGER*)&currTimeTmp);

		stopTime = currTimeTmp;
		stopped = true;
	}
}

void Timer::Tick()
{
	if(stopped)
	{
		deltaTime = 0;
		return;
	}

	__int64 currTimeTmp;
	QueryPerformanceCounter((LARGE_INTEGER*)&currTimeTmp);
	this->currTime = currTimeTmp;

	deltaTime = (currTime - prevTime) * secondsPerCount;

	prevTime = currTime;

	if(deltaTime < 0.0)
	{
		deltaTime = 0;
	}
}